﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace _069.EFCore
{
	public class TestDbContext : DbContext
	{
		public static ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddConsole());
		public DbSet<Book> Books { get; set; }
		public DbSet<Person> Persons { get; set; }
		public DbSet<Dog> Dogs { get; set; }
		public DbSet<Cat> Cats { get; set; }
		protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
		{
			base.OnConfiguring(optionsBuilder);
			string connStr = "Server=10.34.10.12,1433;Database=fanzhineng;uid=fzn;Password=Ub6PjWV5r8J0&X!b;";
			optionsBuilder.UseSqlServer(connStr);

			// 方法一 标志日志方式
			//optionsBuilder.UseLoggerFactory(loggerFactory);

			// 方法二 简单日志
			// optionsBuilder.LogTo(Console.WriteLine);
			/*optionsBuilder.LogTo(msg => {
				if (!msg.Contains("CommandExecuted"))
				{
					return;
				}
				Console.WriteLine(msg);
			});*/

			// 方法三 ToQueryString  groups.ToQueryString();

		}
		protected override void OnModelCreating(ModelBuilder modelBuilder)
		{
			base.OnModelCreating(modelBuilder);
			// 从当前程序集加载所有的IEntityTypeConfiguration
			modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);

		}

	}
}
